package com.mb.order.controller;

import com.mb.common.pojo.Order;
import com.mb.common.vo.SysResult;
import com.mb.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("order/manage")
public class OrderController {
    @Autowired
    private OrderService orderService;
     /*
    新增订单
    请求地址:/save
    请求参数:Order order key=value&key=value
    返回数据:SysResult 200成功
     */
    @RequestMapping("/save")
    public SysResult addorder(Order order, HttpServletResponse response){
        response.setContentType("text/html;charset=utf-8");
        try{
            orderService.addOrder(order);
            return SysResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return SysResult.build(201,"新增订单失败",null);
        }
    }
    /*
        请求地址:/query/{userId}
        请求参数:路径参数String userId
        返回数据:List<Order>
     */
    @RequestMapping("/query/{userId}")
    public List<Order> queryMyOrders(@PathVariable String userId){
        return orderService.queryMyOrders(userId);
    }
    /*
        请求地址：/delete/{orderId}
        请求参数：路径参数String orderId
        返回数据：SysResult成功失败
        delete from t_order where
        delete from t_order_item where
     */
    @RequestMapping("/delete/{orderId}")
    public SysResult deleteOrder(@PathVariable String orderId){
        try{
            orderService.deleteOrder(orderId);
            return SysResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return SysResult.build(201,"删除失败",null);
        }
    }
}
